package com.ruoyi.manager.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.manager.domain.ReservationDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manager.domain.TReservation;
import com.ruoyi.manager.service.ITReservationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/manager/reservation")
public class TReservationController extends BaseController
{
    @Autowired
    private ITReservationService tReservationService;

    /**
     * 查询预约管理列表
     */
    @PreAuthorize("@ss.hasPermi('manager:reservation:list')")
    @GetMapping("/list")
    public TableDataInfo list(TReservation tReservation)
    {
        startPage();
        List<TReservation> list = tReservationService.selectTReservationList(tReservation);
        return getDataTable(list);
    }

    /**
     * 导出预约管理列表
     */
    @PreAuthorize("@ss.hasPermi('manager:reservation:export')")
    @Log(title = "预约管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TReservation tReservation)
    {
        List<TReservation> list = tReservationService.selectTReservationList(tReservation);
        ExcelUtil<TReservation> util = new ExcelUtil<TReservation>(TReservation.class);
        util.exportExcel(response, list, "预约管理数据");
    }

    /**
     * 获取预约管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tReservationService.selectTReservationById(id));
    }

    /**
     * 新增预约管理
     */
    @PreAuthorize("@ss.hasPermi('manager:reservation:add')")
    @Log(title = "预约管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TReservation tReservation)
    {
        return toAjax(tReservationService.insertTReservation(tReservation));
    }

    /**
     * 修改预约管理
     */
    @PreAuthorize("@ss.hasPermi('manager:reservation:edit')")
    @Log(title = "预约管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TReservation tReservation)
    {
        return toAjax(tReservationService.updateTReservation(tReservation));
    }

    /**
     * 删除预约管理
     */
    @PreAuthorize("@ss.hasPermi('manager:reservation:remove')")
    @Log(title = "预约管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tReservationService.deleteTReservationByIds(ids));
    }

    /**
     * check 检查所选时间段是否有占用
     */
    @GetMapping("/check")
    public boolean checkTimes(Long placeId , String startTime , Integer duration){
        //解析为LocalDateTime
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = start.plusMinutes(duration);

        return tReservationService.checkTimes(placeId, start, end);
    }

    /**
     * 用户新增预约
     */
    @PostMapping("userAdd")
    public AjaxResult userAddReservation(@RequestBody ReservationDto reservationDto){

        int res = tReservationService.insertUserTReservation(reservationDto);
        if(res == -1)
            return AjaxResult.error("用户不存在");

        return AjaxResult.success(res);
    }

}
